﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class D04_Resources : MonoBehaviour
{
    private GameObject prefab;
    private Texture texture;
    private void Start()
    {
        //加载预制体
        prefab = Resources.Load<GameObject>("Prefab/Cube");

        //实例化
        GameObject cube = Instantiate(prefab, Vector3.zero, Quaternion.identity);

        //加载Image 并贴到方块上
        texture = Resources.Load<Texture>("Texture/image1");
        cube.GetComponent<MeshRenderer>().material.mainTexture = texture;

        TextAsset txt = Resources.Load<TextAsset>("新建文本文档");
        cube.name = txt.text;

        TextAsset txt1 = Resources.Load<TextAsset>("新建文本文档");

        print(ReferenceEquals(txt, txt1));

        GameObject prefab1 = Resources.Load<GameObject>("Prefab/Cube");
        print(ReferenceEquals(prefab, prefab1));
    }
    //注意Resources.Load 会缓存之前加载的资源,如果再次加载则返回之前加的.不需要我们再去做缓存优化
    private void Start1()
    {
        //加载所有资源,加载所有相同路径下的所有资源,包括子文件夹

        UnityEngine.Object[] resoucesArray = Resources.LoadAll("/");
        foreach (var item in resoucesArray)
        {
            print(item.name);
        }
        print(resoucesArray.Length);
    }

    //注意预制体不能被卸载
    private void OnGUI()
    {
        if (GUILayout.Button("卸载texture"))
        {
            Resources.UnloadAsset(texture);
        }
        if (GUILayout.Button("加载texture"))
        {
            Resources.Load<Texture>("Texture/image1");
        }
    }
}